<template>
    <div class="list-page">
        <div class="list-page-header list-page-header-flex">
            <div class="flex-left">
                <el-button type="primary" @click="add">新增</el-button>
                <el-button type="primary" @click="edit">编辑</el-button>
                <el-popconfirm title="是否继续？" confirmButtonText="确定" cancelButtonText="取消" @confirm="del">
                    <template #reference>
                        <el-button>删除</el-button>
                    </template>
                </el-popconfirm>
            </div>
            <div class="flex-right">
                <el-input v-model="query.projectName" class="search-input-class" placeholder="项目名称" />
                <el-input v-model="query.departmentName" class="search-input-class" placeholder="部门名称" />
                <el-button type="primary" @click="loadData">查询</el-button>
                <el-button @click="reset">重置</el-button>
            </div>
        </div>
        <div class="list-content">
            <el-table
                ref="datagrid"
                :data="table.data"
                border
                fit
                @row-click="handleClickTableRow"
                min-height="300"
                @selection-change="handleSelectionChange"
            >
                <el-table-column type="selection" width="55" />
                <el-table-column prop="projectName" label="项目名" min-width="300" show-overflow-tooltip />
                <el-table-column prop="departmentName" label="部门名称" min-width="160" show-overflow-tooltip />
                <el-table-column prop="creator" label="填写人" min-width="120" />
                <el-table-column prop="createTime" label="填写时间" min-width="180" show-overflow-tooltip />
            </el-table>
        </div>
        <div class="list-footer">
            <el-pagination
                background
                @size-change="handleSizeChange"
                @current-change="handleCurrentChange"
                :current-page="table.pagination.currentPage"
                :page-sizes="table.pagination.pageSizes"
                :page-size="table.pagination.pageSize"
                layout="total, sizes, prev, pager, next, jumper"
                :total="table.pagination.total"
            ></el-pagination>
        </div>
        <transition name="slide-fade">
            <div v-if="showInfoWindow" class="list-form-modal">
                <ActivityForm :id="selectedId" @close="closeInfoWindow"></ActivityForm>
            </div>
        </transition>
    </div>
</template>
<script setup>
import { ref, reactive, onMounted } from 'vue'
import { ElNotification } from 'element-plus'
import ActivityForm from './form/ActivityForm.vue'
import http from '@/http.js'

const datagrid = ref(null)

const query = reactive({
    projectName: '',
    departmentName: ''
})

const table = reactive({
    data: [],
    selected: [],
    pagination: {
        currentPage: 1,
        pageSizes: [10, 20, 50, 100],
        pageSize: 10,
        total: 0
    }
})

const selectedId = ref('')

const showInfoWindow = ref(false)

const handleSelectionChange = (val) => {
    table.selected = val
}

const handleSizeChange = (val) => {
    table.pagination.pageSize = val
    loadData()
}

const handleCurrentChange = (val) => {
    table.pagination.currentPage = val
    loadData()
}

const handleClickTableRow = (row) => {
    datagrid.value.clearSelection()
    datagrid.value.toggleRowSelection(row)
}

const loadData = () => {
    let params = {
        projectName: query.projectName,
        departmentName: query.departmentName,
        page: table.pagination.currentPage,
        pageSize: table.pagination.pageSize
    }
    http.fetchForm({
        url: '/jingying/activity/plan/grid',
        data: params
    }).then((res) => {
        table.data = res.data.grid
        table.pagination.total = res.data.total
    })
}

const reset = () => {
    query.projectName = ''
    query.departmentName = ''
    loadData()
}

const add = () => {
    selectedId.value = ''
    showInfoWindow.value = true
}

const edit = () => {
    let selected = table.selected
    if (selected.length == 0) {
        ElNotification({
            title: '提示',
            message: '请选择要编辑的活动',
            type: 'info'
        })
        return
    }
    selectedId.value = selected[0].id
    showInfoWindow.value = true
}

const del = () => {
    let selected = table.selected
    if (selected.length == 0) {
        ElNotification({
            title: '提示',
            message: '请选择要删除的活动',
            type: 'info'
        })
        return
    }
    http.fetchForm({
        url: '/jingying/activity/plan/deleteById',
        method: 'post',
        data: {
            id: selected[0].id
        }
    }).then((res) => {
        ElNotification({
            title: '提示',
            message: '删除成功',
            type: 'success'
        })
        loadData()
    })
}

const closeInfoWindow = () => {
    showInfoWindow.value = false
    loadData()
}

// mounted
onMounted(() => {
    loadData()
})
</script>
<style scoped>
@import '@/assets/css/list-page.css';
</style>
